{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from time import time\n",
    "import glob\n",
    "import json\n",
    "from sklearn.preprocessing import MinMaxScaler, StandardScaler\n",
    "import pickle\n",
    "\n",
    "from arimafd import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "all_files=[]\n",
    "import os\n",
    "for root, dirs, files in os.walk(r'data/'):\n",
    "    for file in files:\n",
    "        if file.endswith(\".csv\"):\n",
    "             all_files.append(os.path.join(root, file))\n",
    "with open(r'labels\\combined_windows.json') as tt:\n",
    "    accidents= json.load(tt)\n",
    "accidents[all_files[0][5:].replace('\\\\','/')]\n",
    "\n",
    "def rl(x):\n",
    "    return range(len(x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Compute Arima weights coeffecients \n",
    "output - 3d numpy array for every dataset\n",
    "* time moment\n",
    "* specific 1d time series\n",
    "* lag of autoregressive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time seconds: 40.49042344093323\n",
      "Time seconds: 41.29134225845337\n",
      "Time seconds: 42.0969352722168\n",
      "Time seconds: 44.81729745864868\n",
      "Time seconds: 44.01752185821533\n",
      "Time seconds: 41.73517179489136\n",
      "Time seconds: 40.03299283981323\n",
      "Time seconds: 40.88009023666382\n",
      "Time seconds: 41.13068628311157\n",
      "Time seconds: 46.30257034301758\n",
      "Time seconds: 43.722044467926025\n",
      "Time seconds: 15.104308366775513\n",
      "Time seconds: 16.63646697998047\n",
      "Time seconds: 14.693701028823853\n",
      "Time seconds: 13.040173292160034\n",
      "Time seconds: 14.402956485748291\n",
      "Time seconds: 14.271668195724487\n",
      "Time seconds: 43.21866250038147\n",
      "Time seconds: 43.509324073791504\n",
      "Time seconds: 43.31928062438965\n",
      "Time seconds: 45.94165539741516\n",
      "Time seconds: 41.88232064247131\n",
      "Time seconds: 42.41168665885925\n",
      "Time seconds: 42.537001848220825\n",
      "Time seconds: 41.60645079612732\n",
      "Time seconds: 50.43983054161072\n",
      "Time seconds: 42.05333471298218\n",
      "Time seconds: 43.411460876464844\n",
      "Time seconds: 52.738277196884155\n",
      "Time seconds: 44.49322056770325\n",
      "Time seconds: 50.37369108200073\n",
      "Time seconds: 10.849128484725952\n",
      "Time seconds: 46.60483646392822\n",
      "Time seconds: 45.978679180145264\n",
      "Time seconds: 90.8661572933197\n",
      "Time seconds: 287.84452986717224\n",
      "Time seconds: 43.98499417304993\n",
      "Time seconds: 402.27064275741577\n",
      "Time seconds: 134.099702835083\n",
      "Time seconds: 17.864099740982056\n",
      "Time seconds: 61.119014501571655\n",
      "Time seconds: 23.90894603729248\n",
      "Time seconds: 26.157429218292236\n",
      "Time seconds: 24.17434525489807\n",
      "Time seconds: 10.43119478225708\n",
      "Time seconds: 26.148040294647217\n",
      "Time seconds: 24.116364002227783\n",
      "Time seconds: 22.148979425430298\n",
      "Time seconds: 241.8483443260193\n",
      "Time seconds: 241.15672326087952\n",
      "Time seconds: 231.52628374099731\n",
      "Time seconds: 231.9701611995697\n",
      "Time seconds: 237.3497281074524\n",
      "Time seconds: 233.5730414390564\n",
      "Time seconds: 239.28472781181335\n",
      "Time seconds: 235.94901609420776\n",
      "Time seconds: 234.2280478477478\n",
      "Time seconds: 233.5527367591858\n"
     ]
    }
   ],
   "source": [
    "tensors = []\n",
    "for i in rl(all_files):\n",
    "    df = pd.read_csv(all_files[i],index_col = 'timestamp', parse_dates=True)\n",
    "    a = anomaly_detection(df)\n",
    "    tensors.append(a.generate_tensor(ar_order=100))\n",
    "\n",
    "with open(r'C:\\data\\pickle_arima\\numenta\\tensors.pickle', 'wb') as handle:\n",
    "    pickle.dump(tensors, handle, protocol=pickle.HIGHEST_PROTOCOL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
